/*******************************************************************************
 *
 *      Filename: test_Application.cpp
 *   Description: Test for Application class
 *        Author: [kb]ypp
 *    Created at: December 1, 2005
 * Last modified: 
 *
 ******************************************************************************/

#include "../Application.h"
#include "../Log.h"
#include "../Scene.h"
#include <GL/gl.h>
#include <GL/glu.h>

class TestScene : public Scene {
public:
	TestScene() : cylinder( 0 ), xrot( 0.0 ), yrot( 0.0 )
	{
		cylinder = glGenLists( 1 );
		GLUquadricObj *qObj = gluNewQuadric();
		gluQuadricDrawStyle( qObj, GLU_FILL );
		gluQuadricNormals( qObj, GLU_SMOOTH );
		glNewList( cylinder, GL_COMPILE );
			gluCylinder( qObj, 0.5, 0.4, 1.0, 15, 5 );
		glEndList();
		gluDeleteQuadric( qObj );
	}
	
	~TestScene()
	{
		glDeleteLists( cylinder, 1 );
	}
	
private:
	virtual void doInit()
	{
		glClearColor( 0.0, 0.0, 0.0, 0.0 );
		glShadeModel( GL_SMOOTH );
		glEnable( GL_DEPTH_TEST );
		
		glColor3f( 1.0, 0.5, 0.25 );
	}
	
	virtual void doUpdate( float deltaT )
	{
		xrot += deltaT * 90.0;
		yrot += deltaT * 120.0;
	}
	
	virtual void doRender()
	{
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		
		glPushMatrix();
			glRotatef( xrot, 1.0, 0.0, 0.0 );
			glRotatef( yrot, 0.0, 1.0, 0.0 );
			
			glBegin( GL_QUADS );
				glVertex3f( -1.0, -1.0, 0.0 );
				glVertex3f(  1.0, -1.0, 0.0 );
				glVertex3f(  1.0,  1.0, 0.0 );
				glVertex3f( -1.0,  1.0, 0.0 );
			glEnd();
			
			glCallList( cylinder );
		glPopMatrix();
	}
	
	virtual void doCleanup()
	{
	}
	
	GLsizei cylinder;
	GLfloat xrot, yrot;
};

void resize( int w, int h )
{
    glViewport( 0, 0, w, h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 90.0, static_cast< GLdouble >( w ) / h, 0.1, 20.0 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0.0, 0.0, -2.5 );
}

int main()
{
	Application app( "Application test", 800, 600, true );
	TestScene test;
	
	resize( 800, 600 );
	
	app.addScene( &test );
	app.run();
	
	return 0;
}
